(* file: lexer.mll *)

{
  open Turtle
  open Lexing
  open Hashtbl

  let basic_cmd = Hashtbl.create 2
  let _ =
    List.iter (fun (kwd, tok) -> Hashtbl.add basic_cmd kwd tok)
      [ "MOVE", MOVETOK;
	"TURN", TURNTOK;
      ]
}

let digits = '-'? ['0'-'9']+

rule token = parse
  | [' ' '\t' '\n' ]	{ token lexbuf }
  | ';'         { SEMICOLON }
  | '['         { LEFTBRACKET }
  | ']'         { RIGHTBRACKET }
  | digits as num
		{ NUM (float_of_string num) }

  | ['A'-'Z' 'a'-'z'] ['A'-'Z' 'a'-'z' '0'-'9' '_'] * as id
      { try 
	  Hashtbl.find basic_cmd id
	with Not_found -> 
	  print_endline ("\t"^id^": unknown cmd");
	  token lexbuf
      }
      
  | _		{ token lexbuf }
  | eof		{ raise End_of_file }

